package com.blq.developtools.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.EventLog;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blq.developtools.interfaces.OnFloatWindowTouchListener;

/**
 * <pre>
 * ****************************************************************
 * 文件名称:FloatWrapFrameLayout.java
 * 作 者:SSNB
 * 创建时间:2024/5/21
 * 文件描述:
 * 注意事项:
 * 版权声明:Copyright (C) 2015-2025 杭州中焯信息技术股份有限公司
 * 修改历史:2024/5/21 1.00 初始版本
 * ****************************************************************
 * </pre>
 */
public class FloatWrapFrameLayout extends FrameLayout {
    public FloatWrapFrameLayout(@NonNull Context context) {
        this(context, null);
    }

    public FloatWrapFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FloatWrapFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public FloatWrapFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
//        setBackgroundColor(0xaF00FF00);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

    }

    private OnFloatWindowTouchListener onFloatWindowTouchListener;
    private boolean canDrag = false;

    public void setCanDrag(boolean enable) {
        this.canDrag = enable;
    }

    public boolean isCanDrag() {
        return canDrag;
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        sendTouch(ev);
        return canDrag || super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        sendTouch(event);
        return canDrag || super.onTouchEvent(event);
    }

    private void sendTouch(MotionEvent event) {
        if (event != null) {
            if (onFloatWindowTouchListener != null) {
                onFloatWindowTouchListener.onTouch(event);
            }
        }
    }

    @Override
    public boolean dispatchKeyEventPreIme(KeyEvent event) {
        return super.dispatchKeyEventPreIme(event);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    public void setOnFloatWindowTouchListener(OnFloatWindowTouchListener onFloatWindowTouchListener) {
        this.onFloatWindowTouchListener = onFloatWindowTouchListener;
    }
}
